﻿Imports Microsoft.VisualBasic
Imports Aras.IOM
Imports MSXML2
Imports System.Xml
Imports System.IO



Public Class ConnecteurAras

    Public state As Boolean
    Public IdentityID As String
    Public UserID As String
    Public ActiveProjectID As String
    Dim conn As HttpServerConnection
    Dim inn As Innovator
    Private url As String
    Private db As String
    Private VaultUrl As String




    Public Sub New(ByVal UserName As String, ByVal UserPass As String)

        Me.state = False
        url = "http://prodeosvpn.no-ip.org/Prodeos/Server/InnovatorServer.aspx"
        VaultUrl = "http://prodeosvpn.no-ip.org/Prodeos/Vault/VaultServer.aspx"
        db = "Navins-Dev"
        Dim InnovUser As String = UserName
        Dim pass As String = UserPass
        conn = Nothing


        If pass = "" Or InnovUser = "" Or pass = Nothing Or InnovUser = Nothing Then
            pass = "false"
            InnovUser = "false"
        End If

        pass = Innovator.ScalcMD5(pass)
        conn = IomFactory.CreateHttpServerConnection(url, db, InnovUser, pass)



        Dim login_result As Item = conn.Login


        If login_result.isError Then
            Me.state = False
            UserName = "false"
            logActivity("error", "Erreur au login", login_result.getErrorString)
            GoTo Errormanagement
            'Throw New Exception("login failed")

        End If
        Me.state = True
        inn = IomFactory.CreateInnovator(conn)
        logActivity("Success", "Successfull login", "Utilisateur = " + UserName)
        Dim qryitem As Item = inn.newItem("User", "get")
        qryitem.setProperty("login_name", UserName)
        Dim rel1 As Item = inn.newItem("Alias", "get")
        Dim subfolder1 As Item = inn.newItem("Identity", "get")
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)
        Dim results As Item = qryitem.apply()
        Me.state = True

        Me.IdentityID = results.getItemsByXPath(results.XPathResultItem).getRelationships("Alias").getItemByIndex(0).getRelatedItemID
        Me.UserID = results.getItemsByXPath(results.XPathResultItem).getID

Errormanagement:
    End Sub

    Public Sub logActivity(ByVal Activity_Type As String, _
                           ByVal Activity_Name As String, _
                           ByVal Activity_Description As String)

        Dim XMLd As New XmlDocument
        Dim XMLN As XmlNode
        Dim websitepath As String = System.AppDomain.CurrentDomain.BaseDirectory()
        Dim strPath As String = websitepath & "Log\Log_" + DateAndTime.DateString + ".xml"


        'and xmldocument, to represent the instance of the XML file we're dealing with.
        Try
            XMLd.Load(strPath)
        Catch ex As Exception
            XMLd.Load(websitepath & "Log\Log_template.xml")
        End Try


        'An XML Element <name>
        Dim xmlEl As XmlElement = XMLd.CreateElement("Activity")



        'What goes INside the element we just made. 
        xmlEl.InnerXml = "<activity_type></activity_type>"
        xmlEl.InnerXml += "<TimeStamp></TimeStamp>"
        xmlEl.InnerXml += "<activity_name></activity_name>"
        xmlEl.InnerXml += "<activity_Description></activity_Description>"

        'give it some values
        xmlEl.Item("activity_type").InnerText = Activity_Type
        xmlEl.Item("TimeStamp").InnerText = DateAndTime.Now
        xmlEl.Item("activity_name").InnerText = Activity_Name

        'put space in xml output
        Activity_Description.Replace("><", "> <")
        Activity_Description.Replace("&gt;", "&gt; ")
        Activity_Description.Replace("&lt;", " &lt;")
        xmlEl.Item("activity_Description").InnerText = Activity_Description


        'append the whole element to the whole damn document. 
        XMLd.DocumentElement.AppendChild(xmlEl)
        'save the document now!

        XMLd.Save(websitepath & "Log\Log_" + DateAndTime.DateString + ".xml")


        'navigation
        'delete node


    End Sub



    Public Function NotifyforNewFile(ByVal fileid As String, ByVal type As String, ByVal ID_project As String)


    End Function


    Public Function addfileToProject(ByVal fileid As String, ByVal ID_project As String) As String

        Dim results As Item
        Dim qryitem, qryitem1, qryitem2 As Item


        'suppression de la designation du manager du document
        qryitem = Me.inn.newItem("Document", "edit")
        qryitem.setAttribute("where", "[Document].id='" & fileid & "'")
        qryitem.setProperty("managed_by_id", "")
        results = qryitem.apply()

        If results.isError Then
            logActivity("error", "Erreur du retrait de la gestion du fichier", results.getErrorString)
        Else
            logActivity("Success", "Retrait de propriété", "Le fichier: " + fileid + " n'est plus géré par son créateur")
        End If

        'ajout du document à un projet
        qryitem = Me.inn.newItem("Project", "edit")
        qryitem.setAttribute("where", "[Project].id='" & ID_project & "'")
        qryitem1 = Me.inn.newItem("Project Docs", "add")

        qryitem2 = Me.inn.newItem("Document", "get")
        qryitem2.setProperty("id", fileid)
        qryitem1.setRelatedItem(qryitem2)
        qryitem.addRelationship(qryitem1)


        results = qryitem.apply()
        If results.isError Then
            logActivity("error", "Erreur d'ajout du document", results.getErrorString)
        Else
            logActivity("Success", "Document ajouté au projet", "Le fichier: " + fileid + ", a bien été rajouté au projet: " + ID_project)
        End If

        Return results.isError

    End Function


    Public Function GetProjectFiles(ByVal ID_Project As String) As String

        Dim qryitem As Item = Me.inn.newItem("Project", "get")
        qryitem.setProperty("id", ID_Project)
        Dim rel1 As Item = Me.inn.newItem("Project Docs", "get")
        Dim subfolder1 As Item = Me.inn.newItem("Document", "get")
        Dim rel2 As Item = Me.inn.newItem("Document File", "get")
        Dim subfolder2 As Item = Me.inn.newItem("File", "get")


        rel2.setRelatedItem(subfolder2)
        subfolder1.addRelationship(rel2)
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument
        Dim phraseretour As String
        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0
        Dim userFound As Item
        Dim UserName As String
        Dim filefound As Item
        Dim filename As String


        Dim item As Item = results.getItemByIndex(0)
        Try

            countsub = item.getRelationships.getItemCount

        Catch ex As Exception
            GoTo NoFichier
        End Try
        For a = 0 To countsub - 1
            Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)
            Try

                Dim subitem3 As Item = subitem2.getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0)



                Dim qryUser As Item = Me.inn.newItem("User", "get")
                qryUser.setProperty("id", subitem2.getProperty("created_by_id"))
                userFound = qryUser.apply()
                userFound = userFound.getItemsByXPath(userFound.XPathResultItem).getItemByIndex(0)
                UserName = userFound.getProperty("first_name") & userFound.getProperty("last_name")

                Dim getfilename As Item = Me.inn.newItem("Document", "get")
                getfilename.setProperty("id", subitem2.getID)
                filefound = getfilename.apply()
                filefound = filefound.getItemsByXPath(filefound.XPathResultItem).getItemByIndex(0)
                filename = filefound.getProperty("name")

                phraseretour += "<row id='" & subitem2.getID & "'>"

                If (Len(filename) > 35) Then
                    phraseretour += "<Nom>" & Left(filename, 32) & "...</Nom>"
                Else

                    phraseretour += "<Nom>" & Left(filename, 35) & "</Nom>"
                End If


                phraseretour += "<Description>" & subitem2.getProperty("description") & "</Description>"
                phraseretour += "<Version>" & subitem2.getProperty("major_rev") & "</Version>"
                phraseretour += "<Projet>" & item.getProperty("name") & "</Projet>"
                phraseretour += "<Creator>" & UserName & "</Creator>"
                phraseretour += "<Size>" & CStr(CInt(subitem3.getProperty("file_size")) / 1000) & " ko</Size>"
                phraseretour += "<DateOfCreation>" & Split(subitem2.getProperty("created_on"), "T")(0) & "</DateOfCreation>"
                phraseretour += "</row>"
            Catch ex As Exception

            End Try
        Next

NoFichier:
        phraseretour += "</rows>"
        Return phraseretour



    End Function


    Public Function GetMyFiles(ByVal IdNumber As String, ByVal LinkType As String) As String


        Dim subfolder1 As Item = Me.inn.newItem("Document", "get")

        If LinkType = "managed" Then
            subfolder1.setProperty("managed_by_id", IdNumber)
        ElseIf LinkType = "created" Then
            subfolder1.setProperty("created_by_id", IdNumber)
        End If


        Dim rel2 As Item = Me.inn.newItem("Document File", "get")
        Dim subfolder2 As Item = Me.inn.newItem("File", "get")


        rel2.setRelatedItem(subfolder2)
        subfolder1.addRelationship(rel2)

        Dim results As Item = subfolder1.apply()

        Dim traitementXML As DOMDocument = New DOMDocument
        Dim phraseretour As String
        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0
        Dim userFound As Item
        Dim UserName As String

        Dim item As Item = results.getItemByIndex(0)

        Try

            countsub = results.getItemCount

        Catch ex As Exception
            GoTo NoFichier
        End Try
        For a = 0 To countsub - 1
            Try
                Dim subitem1 As Item = results.getItemByIndex(a)
                Dim subitem2 As Item = subitem1.getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0)



                Dim qryUser As Item = Me.inn.newItem("User", "get")
                qryUser.setProperty("id", subitem1.getProperty("created_by_id"))
                userFound = qryUser.apply()
                userFound = userFound.getItemsByXPath(userFound.XPathResultItem).getItemByIndex(0)
                UserName = userFound.getProperty("first_name") & userFound.getProperty("last_name")


                phraseretour += "<row id='" & subitem1.getID & "'>"
                phraseretour += "<Nom>" & subitem1.getProperty("name") & "</Nom>"
                phraseretour += "<Description>" & subitem1.getProperty("description") & "</Description>"
                phraseretour += "<Version>" & subitem1.getProperty("major_rev") & "</Version>"
                phraseretour += "<Projet> - </Projet>"
                phraseretour += "<Creator>" & UserName & "</Creator>"
                phraseretour += "<Size>" & CStr(CInt(subitem2.getProperty("file_size")) / 1000) & " ko</Size>"
                phraseretour += "<DateOfCreation>" & Split(subitem1.getProperty("created_on"), "T")(0) & "</DateOfCreation>"
                phraseretour += "</row>"
            Catch ex As Exception

            End Try

        Next

NoFichier:
        phraseretour += "</rows>"
        Return phraseretour




    End Function

    Public Function deletefile(ByVal FileId As String) As String
        Dim result As String = ""

        Dim qryitem As Item = Me.inn.newItem("Document", "delete")
        qryitem.setAttribute("id", FileId)
        Dim rel1 As Item = Me.inn.newItem("Document File", "delete")
        Dim fileitem As Item = Me.inn.newItem("File", "delete")
        rel1.setRelatedItem(fileitem)
        qryitem.addRelationship(rel1)

        Dim results As Item = qryitem.apply()
        If results.isError() Then
            result = results.getErrorString
        Else
            result = "ok"
        End If

        Return result
    End Function



    Public Function getfichiers(ByVal IDfolder As String) As String


        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "get")
        qryitem.setProperty("id", IDfolder)
        Dim rel1 As Item = Me.inn.newItem("EDM_Folder_Doc", "get")
        Dim subfolder1 As Item = Me.inn.newItem("Document", "get")
        subfolder1.setAttribute("select", " keyed_name, major_rev,name , state, classification,locked_by_id,generation")

        Dim phraseretour As String = "test"
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0
        Dim userFound As Item
        Dim UserName As String

        Dim item As Item = results.getItemByIndex(0)
        Try

            countsub = item.getRelationships.getItemCount

        Catch ex As Exception
            GoTo NoFichier
        End Try
        For a = 0 To countsub - 1
            Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)

            If subitem2.getProperty("locked_by_id") = Nothing Then
                UserName = "Not Locked"
            Else

                Dim qryUser As Item = Me.inn.newItem("User", "get")
                qryUser.setProperty("id", subitem2.getProperty("locked_by_id"))
                userFound = qryUser.apply()
                userFound = userFound.getItemsByXPath(userFound.XPathResultItem).getItemByIndex(0)
                UserName = userFound.getProperty("first_name") & userFound.getProperty("last_name")
            End If

            phraseretour += "<row id='" & subitem2.getID & "'>"
            phraseretour += "<cell>" & subitem2.getProperty("keyed_name") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("major_rev") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("generation") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("name") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("classification") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("state") & "</cell>"
            phraseretour += "<cell>" & UserName & "</cell>"
            phraseretour += "</row>"

        Next

NoFichier:
        phraseretour += "</rows>"
        Return phraseretour


    End Function

    Public Function getTeam(ByVal Project As String) As String


        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "get")
        qryitem.setProperty("id", Project)
        Dim rel1 As Item = Me.inn.newItem("EDM_Folder_Doc", "get")
        Dim subfolder1 As Item = Me.inn.newItem("Document", "get")
        subfolder1.setAttribute("select", " keyed_name, major_rev,name , state, classification")

        Dim phraseretour As String = "test"
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item As Item = results.getItemByIndex(0)
        Try

            countsub = item.getRelationships.getItemCount

        Catch ex As Exception
            GoTo NoFichier
        End Try
        For a = 0 To countsub - 1
            Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)

            phraseretour += "<row id='" & subitem2.getID & "'>"
            phraseretour += "<cell>" & subitem2.getProperty("keyed_name") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("major_rev") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("name") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("classification") & "</cell>"
            phraseretour += "<cell>" & subitem2.getProperty("state") & "</cell>"
            phraseretour += "</row>"

        Next


NoFichier:
        phraseretour += "</rows>"
        Return phraseretour


    End Function

    Public Function getTeamIdentities(ByVal ProjectID As String) As String


        Dim qryitem As Item = Me.inn.newItem("Project", "get")
        qryitem.setProperty("id", ProjectID)
        Dim rel1 As Item = Me.inn.newItem("Project team", "get")
        rel1.setAttribute("select", "role,related_id")
        Dim subfolder1 As Item = Me.inn.newItem("Identity", "get")

        Dim phraseretour As String = "test"
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item, subitem As Item
        Try

            countsub = results.getItemByIndex(0).getRelationships.getItemCount

        Catch ex As Exception
            GoTo NoProject
        End Try
        For a = 0 To countsub - 1
            'Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            'Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)
            item = results.getItemByIndex(0).getRelationships.getItemByIndex(a).getRelatedItem.getItemByIndex(0)
            subitem = results.getItemByIndex(0).getRelationships.getItemByIndex(a)
            phraseretour += "<row id='" & subitem.getID & "'>"
            phraseretour += "<cell>" & item.getProperty("name") & "</cell>"
            phraseretour += "<cell>" & subitem.getProperty("role") & "</cell>"


            phraseretour += "<cell>" & "manager" & "</cell>"
            phraseretour += "</row>"

        Next


NoProject:
        phraseretour += "</rows>"
        Return phraseretour


    End Function

    Public Function getProjects(ByVal IdentityID As String) As String


        Dim qryitem As Item = Me.inn.newItem("Project", "get")
        Dim rel1 As Item = Me.inn.newItem("Project team", "get")
        rel1.setAttribute("select", "role,related_id")
        Dim subfolder1 As Item = Me.inn.newItem("Identity", "get")
        subfolder1.setProperty("id", IdentityID)

        Dim phraseretour As String = "test"
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        Dim results2 As Item
        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item As Item
        Dim item_manager As Item
        Dim Xpathstring As String = results.XPathResultItem

        Try

            countsub = results.getItemsByXPath(Xpathstring).getItemCount

        Catch ex As Exception
            GoTo NoProject
        End Try
        For a = 0 To countsub - 1
            item = results.getItemsByXPath(Xpathstring).getItemByIndex(a)
            'Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            'Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)

            phraseretour += "<row id='" & item.getID & "'>"
            phraseretour += "<name>" & item.getProperty("name") & "</name>"
            phraseretour += "<state>" & item.getProperty("state") & "</state>"

            qryitem = Me.inn.newItem("Identity", "get")
            qryitem.setProperty("id", item.getProperty("managed_by_id"))
            results2 = qryitem.apply()
            item_manager = results.getItemsByXPath(Xpathstring).getItemByIndex(0)

            phraseretour += "<manager>" & item_manager.getProperty("name") & "</manager>"
            phraseretour += "</row>"

        Next


NoProject:
        phraseretour += "</rows>"
        Return phraseretour


    End Function

    Public Function getAllProjects() As String


        Dim qryitem As Item = Me.inn.newItem("Project", "get")


        Dim phraseretour As String = "test"

        Dim results2 As Item
        Dim results As Item = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?>"
        phraseretour += "<rows>"

        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item As Item
        Dim item_manager As Item
        Dim Xpathstring As String = results.XPathResultItem

        Try

            countsub = results.getItemsByXPath(Xpathstring).getItemCount

        Catch ex As Exception
            GoTo NoProject
        End Try
        For a = 0 To countsub - 1
            item = results.getItemsByXPath(Xpathstring).getItemByIndex(a)
            'Dim subitem1 As Item = item.getRelationships.getItemByIndex(a)
            'Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)

            phraseretour += "<row id='" & item.getID & "'>"
            phraseretour += "<cell>" & item.getProperty("name") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("state") & "</cell>"

            qryitem = Me.inn.newItem("Identity", "get")
            qryitem.setProperty("id", item.getProperty("managed_by_id"))
            results2 = qryitem.apply()
            item_manager = results.getItemsByXPath(Xpathstring).getItemByIndex(0)

            phraseretour += "<cell>" & item_manager.getProperty("name") & "</cell>"
            phraseretour += "</row>"

        Next


NoProject:
        phraseretour += "</rows>"
        Return phraseretour


    End Function


    Public Function addfile(ByVal IDfolder As String, ByVal FileName As String, ByVal filepath As String) As String
        Dim result As String = ""

        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "edit")
        qryitem.setAttribute("select", "nomdossier")
        qryitem.setAttribute("where", "[EDM_Folder].id='" & IDfolder & "'")
        Dim rel As Item = Me.inn.newItem("EDM_Folder_Doc", "add")
        Dim subitem As Item = Me.inn.newItem("Document", "add")
        subitem.setProperty("classification", "/Document/Collaboration/Divers")
        subitem.setProperty("name", FileName)
        subitem.setProperty("item_number", FileName)
        Dim docfile As Item = Me.inn.newItem("Document File", "add")
        Dim fileitem As Item = Me.inn.newItem("File", "add")
        fileitem.setProperty("filename", FileName)
        fileitem.attachPhysicalFile(filepath)

        docfile.setRelatedItem(fileitem)
        subitem.addRelationship(docfile)
        rel.setRelatedItem(subitem)
        qryitem.addRelationship(rel)

        Dim results As Item = qryitem.apply()



        'test si existance d'une erreur
        'result = Me.getFolders()


        Return result

    End Function

    Public Function AddDocument(ByVal FileName As String, _
                                ByVal filepath As String, _
                                Optional ByVal Comments As String = "") As String
        Dim result As String = ""

        Dim qryitem As Item = Me.inn.newItem("document", "add")
        qryitem.setProperty("name", FileName)
        qryitem.setProperty("item_number", Innovator.ScalcMD5(FileName + Me.UserID))
        qryitem.setProperty("description", Comments)
        qryitem.setProperty("managed_by_id", Me.UserID)
        Dim docfile As Item = Me.inn.newItem("Document File", "add")
        Dim fileitem As Item = Me.inn.newItem("File", "add")
        fileitem.setProperty("filename", FileName)
        Try

            fileitem.attachPhysicalFile(filepath)
        Catch ex As Exception

        End Try

        docfile.setRelatedItem(fileitem)
        qryitem.addRelationship(docfile)

        Dim results As Item = qryitem.apply()
        result = results.getErrorString
        result = results.getErrorCode
        result = Comments

        Return result

    End Function

    Public Function getRevisions(ByVal IDdoc As String) As String

        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", IDdoc)
        Dim results As Item = qryitem.apply()
        Dim DocumentConfigID As String = results.getItemByIndex(0).getProperty("item_number")

        Dim SQL As String



        qryitem = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("where", "[Document].item_number='" & DocumentConfigID & "'")

        Dim phraseretour As String

        results = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item As Item
        Dim Xpathstring As String = results.XPathResultItem

        Try
            countsub = results.getItemsByXPath(Xpathstring).getItemCount
        Catch ex As Exception
            GoTo NoProject
        End Try
        item = results.getItemsByXPath(Xpathstring).getItemByIndex(0)

        SQL = "SELECT * FROM [innovator].[DOCUMENT] Where [config_id] = '" & item.getProperty("config_id") & "' ORDER BY major_rev, generation "

        results = Me.inn.applySQL(SQL)

        Try
            countsub = results.getItemsByXPath(Xpathstring).getItemCount
        Catch ex As Exception
            GoTo NoProject
        End Try



        For a = 0 To countsub - 1

            item = results.getItemsByXPath(Xpathstring).getItemByIndex(a)
            phraseretour += "<row id='" & item.getID & "'>"
            phraseretour += "<cell>" & item.getProperty("created_on") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("major_rev") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("generation") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("keyed_name") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("state") & "</cell>"
            phraseretour += "</row>"

        Next


NoProject:
        phraseretour += "</rows>"
        Return phraseretour


    End Function

    Public Function getHistory(ByVal IDdoc As String) As String

        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", IDdoc)
        Dim results As Item = qryitem.apply()
        Dim DocumentName As String = results.getItemByIndex(0).getProperty("name")

        qryitem = Me.inn.newItem("History Container", "get")
        qryitem.setAttribute("where", "[History_Container].item_keyed_name='" & DocumentName & "'")

        Dim rel1 As Item = Me.inn.newItem("History", "get")
        rel1.setAttribute("select", "modified_on,modified_by_id,action,item_state,item_version,item_major_rev")

        Dim phraseretour As String = ""

        qryitem.addRelationship(rel1)


        results = qryitem.apply()

        Dim traitementXML As DOMDocument = New DOMDocument

        phraseretour = ""
        phraseretour += "<?xml version='1.0' encoding='utf-8' ?><rows>"
        Dim a As Integer
        Dim countsub As Integer = 0

        Dim item As Item
        Dim Xpathstring As String = results.XPathResultItem

        Try

            countsub = results.getItemsByXPath(Xpathstring).getItemByIndex(0).getRelationships.getItemCount

        Catch ex As Exception
            GoTo NoProject
        End Try
        For a = 0 To countsub - 1
            item = results.getItemsByXPath(Xpathstring).getItemByIndex(0).getRelationships.getItemByIndex(a)

            phraseretour += "<row id='" & item.getID & "'>"
            phraseretour += "<cell>" & item.getProperty("modified_on") & "</cell>"
            phraseretour += "<cell>" & item.getPropertyAttribute("modified_by_id", "keyed_name") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("action") & "</cell>"
            phraseretour += "<cell></cell>"
            phraseretour += "<cell>" & item.getProperty("item_state") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("item_major_rev") & "</cell>"
            phraseretour += "<cell>" & item.getProperty("item_version") & "</cell>"
            phraseretour += "</row>"

        Next


NoProject:
        phraseretour += "</rows>"
        Return phraseretour

    End Function

    Public Function PromoteDocument(ByVal DocID As String, ByVal NextState As String, ByVal Comments As String) As String
        Dim resultat As String

        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", DocID)
        qryitem.apply()
        qryitem.promote(NextState, "")

        If qryitem.isError Then
            resultat = "erreur"
        Else
            resultat = "ok"
        End If

        Return resultat
    End Function

    Public Function GetNextStates(ByVal DocID As String) As Item
        Dim results As Item
        Dim LC_StateID As String = ""

        'récupération de l'ID du statut actuel du lifecycle
        Dim qryitem As Item = Me.inn.newItem("Document", "get")
        qryitem.setAttribute("id", DocID)
        results = qryitem.apply()
        LC_StateID = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getProperty("current_state")

        'récupération de l'ID du lifecycle map
        Dim qryitem_map As Item = Me.inn.newItem("Life Cycle Map", "get")
        Dim qryitem_trans As Item = Me.inn.newItem("Life Cycle Transition", "get")
        qryitem_trans.setAttribute("where", "[LIFE_CYCLE_TRANSITION].from_state = '" & LC_StateID & "'")
        qryitem_map.addRelationship(qryitem_trans)

        results = qryitem_map.apply()

        Return results
    End Function

    ' open file crée une copie du fichier du vault dans un dossier temporaire et renvoi l'URL de ce dernier
    Public Function openfile(ByVal DocumentID As String) As String

        Dim resultat As String
        Dim subitem As Item = Me.inn.newItem("Document", "get")
        subitem.setAttribute("where", "[Document].config_id='" & DocumentID & "'")
        Dim docfile As Item = Me.inn.newItem("Document File", "get")
        Dim fileitem As Item = Me.inn.newItem("File", "get")
        docfile.setRelatedItem(fileitem)
        subitem.addRelationship(docfile)

        Dim results As Item = subitem.apply()

        If results.isError Then

            resultat = "erreur"
        Else

            Dim filename As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getProperty("filename")
            Dim fileID As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getID


            Dim SourceFileAddress As String = Me.VaultUrl & "?dbname=" & Me.db & "&fileID=" & fileID & "&filename=" & filename
            Dim websitepath As String = System.AppDomain.CurrentDomain.BaseDirectory()
            Dim Destination As String = websitepath & "VaultTemp/" & filename

            My.Computer.Network.DownloadFile(SourceFileAddress, Destination, "", "", False, 1000000, True)
            resultat = "VaultTemp/" & filename

        End If




        Return resultat

    End Function

    ' NON-UTILISé - check out file crée une copie du fichier du vault dans un dossier temporaire et renvoi l'URL de ce dernier et lock le File!!
    Private Function checkoutfile_Obsolete(ByVal DocumentID As String) As String

        Dim resultat As String
        Dim subitem As Item = Me.inn.newItem("Document", "get")
        subitem.setAttribute("where", "[Document].id='" & DocumentID & "'")
        Dim docfile As Item = Me.inn.newItem("Document File", "get")
        Dim fileitem As Item = Me.inn.newItem("File", "get")
        docfile.setRelatedItem(fileitem)
        subitem.addRelationship(docfile)

        Dim results As Item = subitem.apply()

        If results.isError Then

            resultat = "erreur"
        Else

            Dim filename As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getProperty("filename")
            Dim fileID As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getID

            'lock file:
            Try
                Dim DocToLock As Item = Me.inn.newItem("Document", "Lock")
                subitem.setAttribute("where", "[Document].id='" & DocumentID & "'")
                results = DocToLock.apply()
                Dim FileToLock As Item = Me.inn.newItem("File", "Lock")
                subitem.setAttribute("where", "[File].id='" & fileID & "'")
                results = DocToLock.apply()

            Catch ex As Exception

            End Try

            Dim SourceFileAddress As String = Me.VaultUrl & "?dbname=" & Me.db & "&fileID=" & fileID & "&filename=" & filename
            Dim websitepath As String = System.AppDomain.CurrentDomain.BaseDirectory()
            Dim Destination As String = websitepath & "VaultTemp/" & filename

            My.Computer.Network.DownloadFile(SourceFileAddress, Destination, "", "", False, 1000000, True)
            resultat = "VaultTemp/" & filename

        End If




        Return resultat


    End Function

    ' Lock file & document!!
    Public Function checkoutfile(ByVal DocumentID As String) As String

        Dim resultat As String
        Dim subitem As Item = Me.inn.newItem("Document", "get")
        subitem.setAttribute("where", "[Document].id='" & DocumentID & "'")
        Dim docfile As Item = Me.inn.newItem("Document File", "get")
        Dim fileitem As Item = Me.inn.newItem("File", "get")
        docfile.setRelatedItem(fileitem)
        subitem.addRelationship(docfile)

        Dim results As Item = subitem.apply()

        If results.isError Then

            resultat = "erreur"
        Else

            Dim filename As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getProperty("filename")
            Dim fileID As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getID

            'lock file:
            Try
                Dim DocToLock As Item = Me.inn.newItem("Document", "lock")
                DocToLock.setAttribute("id", DocumentID)
                results = DocToLock.apply()
                Dim FileToLock As Item = Me.inn.newItem("File", "lock")
                FileToLock.setAttribute("id", fileID)
                results = FileToLock.apply()

            Catch ex As Exception

            End Try

            Dim SourceFileAddress As String = Me.VaultUrl & "?dbname=" & Me.db & "&fileID=" & fileID & "&filename=" & filename
            Dim websitepath As String = System.AppDomain.CurrentDomain.BaseDirectory()
            Dim Destination As String = websitepath & "VaultTemp/" & filename

            My.Computer.Network.DownloadFile(SourceFileAddress, Destination, "", "", False, 1000000, True)
            resultat = "VaultTemp/" & filename

        End If




        Return resultat


    End Function

    Public Function checkinfile(ByVal DocumentID As String, _
                                ByVal filename As String, _
                                ByVal filepath As String) As String

        Dim resultat As String = ""
        Dim subitem As Item = Me.inn.newItem("Document", "get")
        subitem.setAttribute("where", "[Document].id='" & DocumentID & "'")
        Dim docfile As Item = Me.inn.newItem("Document File", "get")
        Dim fileitem As Item = Me.inn.newItem("File", "get")
        docfile.setRelatedItem(fileitem)
        subitem.addRelationship(docfile)

        Dim results As Item = subitem.apply()

        If results.isError Then

            resultat = "erreur"
        Else

            Dim filenamefound As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getProperty("filename")
            Dim fileID As String = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getRelationships.getItemByIndex(0).getRelatedItem.getItemByIndex(0).getID

            If filenamefound = filename Then



                'lock file:
                Try
                    Dim file_to_update As Item

                    'update file
                    subitem = Me.inn.newItem("Document", "edit")
                    subitem.setAttribute("id", DocumentID)
                    docfile = Me.inn.newItem("Document File", "edit")
                    docfile.setAttribute("where", "[Document_File].source_id='" & DocumentID & "'")
                    file_to_update = Me.inn.newItem("File", "add")
                    file_to_update.setProperty("filename", filename)
                    file_to_update.attachPhysicalFile(filepath)


                    docfile.setRelatedItem(file_to_update)
                    subitem.addRelationship(docfile)

                    results = subitem.apply()
                    If results.isError Then
                        MsgBox(results.getErrorDetail)
                    End If


                    'UpdatePanel document
                    Dim Doc_to_update As Item = Me.inn.newItem("Document", "edit")
                    Doc_to_update.setAttribute("id", DocumentID)
                    results = Doc_to_update.apply()

                    'unlock document
                    'Doc_to_update = Me.inn.newItem("Document", "unlock")
                    'Doc_to_update.setAttribute("id", DocumentID)
                    'results = Doc_to_update.apply()


                Catch ex As Exception

                End Try

            Else
                resultat = "mauvais fichier"
                'retour car mauvais fichier selectionné
            End If


            If results.isError Then
                resultat = "erreur"
            Else
                resultat = "OK"
            End If
        End If

        Return resultat


    End Function

    Public Function getFolders(ByVal IdentityID As String, ByVal ProjectID As String) As String


        'recuperation de l'arborescence complète de la GED du projet
        Dim Projitem As Item = Me.inn.newItem("Project", "get")
        Projitem.setAttribute("select", "id")
        Projitem.setProperty("id", ProjectID)
        Dim ProGEDitem As Item = Me.inn.newItem("Project_EDM", "get")
        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "get")
        Projitem.setAttribute("select", "related_id")
        qryitem.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        Dim rel1 As Item = Me.inn.newItem("EDM_subfolder", "get")
        Dim subfolder1 As Item = Me.inn.newItem("EDM_Folder", "get")
        subfolder1.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        Dim rel2 As Item = Me.inn.newItem("EDM_subfolder", "get")
        Dim subfolder2 As Item = Me.inn.newItem("EDM_Folder", "get")
        subfolder2.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        Dim rel3 As Item = Me.inn.newItem("EDM_subfolder", "get")
        Dim subfolder3 As Item = Me.inn.newItem("EDM_Folder", "get")
        subfolder3.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        Dim rel4 As Item = Me.inn.newItem("EDM_subfolder", "get")
        Dim subfolder4 As Item = Me.inn.newItem("EDM_Folder", "get")
        subfolder4.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        Dim rel5 As Item = Me.inn.newItem("EDM_subfolder", "get")
        Dim subfolder5 As Item = Me.inn.newItem("EDM_Folder", "get")
        subfolder5.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        Dim rel6 As Item = Me.inn.newItem("EDM_subfolder", "get")
        Dim subfolder6 As Item = Me.inn.newItem("EDM_Folder", "get")
        subfolder6.setAttribute("select", "id,nomdossier,role1,role2,role3,hasrolerestriction")
        rel6.setRelatedItem(subfolder6)
        subfolder5.addRelationship(rel6)
        rel5.setRelatedItem(subfolder5)
        subfolder4.addRelationship(rel5)
        rel4.setRelatedItem(subfolder4)
        subfolder3.addRelationship(rel4)
        rel3.setRelatedItem(subfolder3)
        subfolder2.addRelationship(rel3)
        rel2.setRelatedItem(subfolder2)
        subfolder1.addRelationship(rel2)
        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)
        ProGEDitem.setRelatedItem(qryitem)
        Projitem.addRelationship(ProGEDitem)
        Dim results As Item = Projitem.apply()


        ' récupération du ou des roles de l'intervenant sur le projet

        Dim reqprojet As Item = Me.inn.newItem("Project", "get")
        reqprojet.setProperty("id", ProjectID)
        Dim reqprojTeam As Item = Me.inn.newItem("Project_EDM", "get")
        Projitem.setAttribute("select", "role")
        Dim reqID As Item = Me.inn.newItem("Identity", "get")
        reqprojet.setProperty("id", IdentityID)

        Dim results2 As Item = Projitem.apply()


        Dim XpathString As String = results2.XPathResultItem
        Dim count As Integer = results.getItemsByXPath(XpathString).getItemByIndex(0).getRelationships.getItemCount
        Dim a, b, c, d, e As Integer
        Dim HasCorrectRole As Boolean = False
        Dim content As String = "<?xml version='1.0' ?><tree id='0'>"
        Dim countsub1, countsub2, countsub4, countsub6 As Integer

        Dim RolesNbOnProj As Integer = 0

        Try
            RolesNbOnProj = results2.getItemsByXPath(XpathString).getItemByIndex(0).getRelationships.getItemCount
        Catch ex As Exception

        End Try

        Dim RolesOnProj(0 To RolesNbOnProj) As String
        For e = 0 To RolesNbOnProj - 1
            RolesOnProj(e) = results2.getItemByIndex(0).getRelationships.getItemByIndex(e).getProperty("role")
        Next


        For a = 0 To count - 1

            Dim item As Item = results.getItemsByXPath(XpathString).getItemByIndex(0).getRelationships.getItemByIndex(a).getRelatedItem.getItemByIndex(0)

            If ((item.getProperty("role1") = "") And (item.getProperty("role2") = "") And (item.getProperty("role3") = "")) Then
                HasCorrectRole = True
            Else
                For e = 0 To RolesNbOnProj - 1
                    If item.getProperty("role1") = RolesOnProj(e) Then HasCorrectRole = True
                    If item.getProperty("role2") = RolesOnProj(e) Then HasCorrectRole = True
                    If item.getProperty("role3") = RolesOnProj(e) Then HasCorrectRole = True
                Next
            End If

            If HasCorrectRole = True Then
                content += "" & _
                "<item id='" & item.getProperty("id") & "' text='" & item.getProperty("nomdossier") & "'>"


                countsub1 = item.getRelationships.getItemCount
                For b = 0 To countsub1 - 1
                    Dim subitem1 As Item = item.getRelationships.getItemByIndex(b)
                    countsub2 = subitem1.getItemCount
                    Dim subitem2 As Item = subitem1.getRelatedItem.getItemByIndex(0)

                    If ((subitem2.getProperty("role1") = "") And (subitem2.getProperty("role2") = "") And (subitem2.getProperty("role3") = "")) Then
                        HasCorrectRole = True
                    Else
                        For e = 0 To RolesNbOnProj - 1
                            If subitem2.getProperty("role1") = RolesOnProj(e) Then HasCorrectRole = True
                            If subitem2.getProperty("role2") = RolesOnProj(e) Then HasCorrectRole = True
                            If subitem2.getProperty("role3") = RolesOnProj(e) Then HasCorrectRole = True
                        Next
                    End If

                    If HasCorrectRole = True Then
                        content += "" & _
                        "<item id='" & subitem2.getProperty("id") & "' text='" & subitem2.getProperty("nomdossier") & "'>"

                        countsub2 = subitem2.getRelationships.getItemCount
                        For c = 0 To countsub2 - 1
                            Dim subitem3 As Item = subitem2.getRelationships.getItemByIndex(c)
                            countsub4 = subitem3.getItemCount
                            Dim subitem4 As Item = subitem3.getRelatedItem.getItemByIndex(0)

                            If ((subitem4.getProperty("role1") = "") And (subitem4.getProperty("role2") = "") And (subitem4.getProperty("role3") = "")) Then
                                HasCorrectRole = True
                            Else
                                For e = 0 To RolesNbOnProj - 1
                                    If subitem4.getProperty("role1") = RolesOnProj(e) Then HasCorrectRole = True
                                    If subitem4.getProperty("role2") = RolesOnProj(e) Then HasCorrectRole = True
                                    If subitem4.getProperty("role3") = RolesOnProj(e) Then HasCorrectRole = True
                                Next
                            End If

                            If HasCorrectRole = True Then
                                content += "" & _
                                "<item id='" & subitem4.getProperty("id") & "' text='" & subitem4.getProperty("nomdossier") & "'>"

                                countsub4 = subitem4.getRelationships.getItemCount
                                For d = 0 To countsub4 - 1
                                    Dim subitem5 As Item = subitem4.getRelationships.getItemByIndex(d)
                                    countsub6 = subitem5.getItemCount
                                    Dim subitem6 As Item = subitem5.getRelatedItem.getItemByIndex(0)

                                    If ((subitem6.getProperty("role1") = "") And (subitem6.getProperty("role2") = "") And (subitem6.getProperty("role3") = "")) Then
                                        HasCorrectRole = True
                                    Else
                                        For e = 0 To RolesNbOnProj - 1
                                            If subitem6.getProperty("role1") = RolesOnProj(e) Then HasCorrectRole = True
                                            If subitem6.getProperty("role2") = RolesOnProj(e) Then HasCorrectRole = True
                                            If subitem6.getProperty("role3") = RolesOnProj(e) Then HasCorrectRole = True
                                        Next
                                    End If

                                    If HasCorrectRole = True Then
                                        content += "" & _
                                        "<item id='" & subitem6.getProperty("id") & "' text='" & subitem6.getProperty("nomdossier") & "'>"

                                        content += "" & _
                                        "</item>"
                                    End If
                                    HasCorrectRole = False
                                Next
                            End If
                            content += "" & _
                            "</item>"
                            HasCorrectRole = False
                        Next
                    End If
                    content += "" & _
                    "</item>"
                    HasCorrectRole = False
                Next
            End If
            content += "" & _
            "</item>"
            HasCorrectRole = False
        Next


        content = content & "</tree>"
        Return content
    End Function

    Public Function getArasRoleList() As Item
        Dim results As Item

        ' construction de la requête AML
        Dim qryitem As Item = Me.inn.newItem("List", "get")
        qryitem.setProperty("name", "Project Role")
        Dim rel1 As Item = Me.inn.newItem("Value", "get")
        qryitem.addRelationship(rel1)

        ' execution de la requête AML
        results = qryitem.apply()

        Return results

    End Function

    Public Function getProjectRoleList(ByVal IdProject As String) As Item
        Dim results As Item

        ' construction de la requête AML
        Dim qryitem As Item = Me.inn.newItem("Project", "get")
        qryitem.setAttribute("id", IdProject)
        Dim rel1 As Item = Me.inn.newItem("Project Team", "get")
        qryitem.setAttribute("select", "role")
        qryitem.addRelationship(rel1)

        ' execution de la requête AML
        results = qryitem.apply()

        Return results

    End Function

    Public Function getProjectId(ByVal ProjectName As String) As String
        Dim resultat As String


        Dim qryitem As Item = Me.inn.newItem("Project", "get")
        qryitem.setAttribute("where", "[Project].name='" & ProjectName & "'")
        Dim results As Item = qryitem.apply()



        If results.isError Then
            resultat = "Erreur"
        Else
            resultat = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getProperty("id")
        End If
        Return resultat

    End Function

    Public Function getProjectName(ByVal ProjectID As String) As String

        Dim resultat As String
        Dim qryitem As Item = Me.inn.newItem("Project", "get")
        qryitem.setAttribute("where", "[Project].id='" & ProjectID & "'")
        Dim results As Item = qryitem.apply()

        If results.isError Then
            resultat = "Erreur"
        Else
            resultat = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getProperty("name")
        End If
        Return resultat

    End Function

    Public Function GetArasUserList() As Item
        Dim results As Item

        ' construction de la requête AML
        Dim qryitem As Item = Me.inn.newItem("Identity", "get")
        qryitem.setProperty("is_alias", "1")

        ' execution de la requête AML
        results = qryitem.apply()

        Return results

    End Function

    Public Function AddRoleToProject(ByVal IdentityName As String, ByVal RoleName As String) As Item
        Dim results As Item
        Dim IdIdentity As String


        'récuperer ID de l'identity
        Dim qryidentity As Item = Me.inn.newItem("Identity", "get")
        qryidentity.setProperty("name", IdentityName)
        qryidentity.setAttribute("select", "id")
        results = qryidentity.apply()
        IdIdentity = results.getItemsByXPath(results.XPathResultItem).getItemByIndex(0).getID()

        'récupérer le projet selectionné
        Dim qryitem As Item = Me.inn.newItem("Project", "edit")
        qryitem.setAttribute("where", "[Project].id='" & Me.ActiveProjectID & "'")
        'ajouter le role selectionné
        Dim rel1 As Item = Me.inn.newItem("Project Team", "add")
        rel1.setProperty("role", RoleName)
        'ajouter l'identité selectionnée
        Dim subfolder1 As Item = Me.inn.newItem("Identity", "get")
        subfolder1.setAttribute("where", "[Identity].id='" & IdIdentity & "'")

        rel1.setRelatedItem(subfolder1)
        qryitem.addRelationship(rel1)

        results = qryitem.apply()

        Return results

    End Function

    Public Function getProjectRoleSettings(ByVal NodeID As String) As Item

        Dim results As Item

        'récuperer ID de l'identity
        Dim qryidentity As Item = Me.inn.newItem("EDM_Folder", "get")
        qryidentity.setAttribute("id", NodeID)
        qryidentity.setAttribute("select", "role1,role2,role3")
        results = qryidentity.apply()

        Return results

    End Function

    Public Function newProject(ByVal project_name As String) As Item
        Dim resultat As Item

        'ajouter le projet
        Dim subitem As Item = Me.inn.newItem("EDM_Folder", "add")
        subitem.setProperty("nomdossier", project_name)

        'ajouter la personne loguée avec le rôle manager


        Return resultat
    End Function

    Public Function UpdateFoldersRoles(ByVal NodeID As String, _
                                       ByVal role1 As String, _
                                       ByVal role2 As String, _
                                       ByVal role3 As String) As Item

        Dim results As Item

        'récuperer ID de l'identity
        Dim qryidentity As Item = Me.inn.newItem("EDM_Folder", "edit")
        qryidentity.setAttribute("id", NodeID)
        qryidentity.setAttribute("select", "role1,role2,role3")
        If role1 = "aucun" Then role1 = Nothing
        If role2 = "aucun" Then role2 = Nothing
        If role3 = "aucun" Then role3 = Nothing
        qryidentity.setProperty("role1", role1)
        qryidentity.setProperty("role2", role2)
        qryidentity.setProperty("role3", role3)
        results = qryidentity.apply()

        Return results

    End Function


    Public Function newfolder(ByVal IDfolder As String, ByVal foldername As String, ByVal isroot As Boolean) As String

        Dim result As String = ""
        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "edit")
        qryitem.setAttribute("select", "nomdossier")
        qryitem.setAttribute("where", "[EDM_Folder].id='" & IDfolder & "'")
        Dim rel As Item = Me.inn.newItem("EDM_subfolder", "add")
        Dim subitem As Item = Me.inn.newItem("EDM_Folder", "add")
        subitem.setProperty("nomdossier", foldername)

        If isroot = False Then
            subitem.setProperty("isrootfolder", "0")
        Else
            subitem.setProperty("isrootfolder", "1")
        End If

        rel.setRelatedItem(subitem)
        qryitem.addRelationship(rel)

        Dim results As Item = qryitem.apply()

        'test si existance d'une erreur
        result = Me.getFolders(Me.IdentityID, Me.ActiveProjectID)


        Return result
    End Function

    Public Function DeleteRoleToProject(ByVal RefRole As String) As Item
        Dim resultat As Item

        'récupérer le projet selectionné
        Dim qryitem As Item = Me.inn.newItem("Project", "edit")
        qryitem.setAttribute("where", "[Project].id='" & Me.ActiveProjectID & "'")
        'ajouter le role selectionné
        Dim rel1 As Item = Me.inn.newItem("Project Team", "delete")
        rel1.setAttribute("where", "[Project_Team].id='" & RefRole & "'")
        qryitem.addRelationship(rel1)

        resultat = qryitem.apply()

        Return resultat
    End Function

    Public Function Deletefolder(ByVal FolderID As String) As Item
        Dim results As Item

        'récupérer le projet selectionné
        Dim qryupitem As Item = Me.inn.newItem("EDM_subfolder", "delete")
        qryupitem.setAttribute("where", "[EDM_Folder].related_id='" & FolderID & "'")
        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "delete")
        qryitem.setAttribute("where", "[EDM_Folder].id='" & FolderID & "'")
        qryupitem.setRelatedItem(qryitem)

        results = qryitem.apply()

        Return results
    End Function

    Public Function Renamefolder(ByVal FolderID As String, ByVal NewName As String) As Item
        Dim results As Item

        'récupérer le projet selectionné
        Dim qryitem As Item = Me.inn.newItem("EDM_Folder", "edit")
        qryitem.setAttribute("where", "[EDM_Folder].id='" & FolderID & "'")
        qryitem.setProperty("nomdossier", NewName)

        results = qryitem.apply()

        Return results
    End Function

    Protected Overrides Sub Finalize()
        conn.Logout()
        MyBase.Finalize()
    End Sub
End Class
